/**
 * Created by Administrator on 2017/8/17 0017.
 */
var User;
if (!User) {
    User = {}
}

User.operateFormatter = function (value, row, index) {
    if (row.isCertification == 1 && row.isCertificate == 0) {
        return [
            '<a class="order" href="javascript:void(0)" >订单列表</a>' +
            '&nbsp;&nbsp;' +
            '<a class="certification" href="javascript:void(0)" style="color: red">实名审核</a>' +
            '&nbsp;&nbsp;' +
            '<a class="details" href="javascript:void(0)" >收支明细</a>' +
            // '&nbsp;&nbsp;' +
            // '<a class="update" href="javascript:void(0)" >修改信息</a>' +
            '&nbsp;&nbsp;' +
            '<a class="reset" href="javascript:void(0)" >重置密码</a>' +
            '&nbsp;&nbsp;' +
            '<a class="company" href="javascript:void(0)" >关联商家</a>'
        ].join('');
    } else if (row.isCertification == 1 && row.isCertificate == 1) {
        return [
            '<a class="order" href="javascript:void(0)" >订单列表</a>' +
            '&nbsp;&nbsp;' +
            '<a class="details" href="javascript:void(0)" >收支明细</a>' +
            '&nbsp;&nbsp;' +
            '<a class="userDetails" href="javascript:void(0)" style="color: #0bb20c">认证详情</a>' +
            '&nbsp;&nbsp;' +
            '<a class="reset" href="javascript:void(0)" >重置密码</a>' +
            '&nbsp;&nbsp;'
            + '<a class="company" href="javascript:void(0)" >关联商家</a>'
            // '<a class="remove" href="javascript:void(0)" style="color: red">删除用户</a>'
        ].join('')
    } else {
        return [
            '<a class="order" href="javascript:void(0)" >订单列表</a>' +
            '&nbsp;&nbsp;' +
            '<a class="details" href="javascript:void(0)" >收支明细</a>' +
            // '&nbsp;&nbsp;' +
            // '<a class="update" href="javascript:void(0)" >修改信息</a>' +
            '&nbsp;&nbsp;' +
            '<a class="reset" href="javascript:void(0)" >重置密码</a>' +
            '&nbsp;&nbsp;'
            + '<a class="company" href="javascript:void(0)" >关联商家</a>'
            // '<a class="remove" href="javascript:void(0)" style="color: red">删除用户</a>'
        ].join('');

    }

};
User.companyOperate = function (value, row, index) {

    return [
        '<a class="companyOrder" href="javascript:void(0)" >查看明细</a>'
    ].join('');
};

/***
 * 操作的点击事件
 */
window.operateEvents = {
    'click .certification': function (e, value, row) {
        showModel("userDetail-div", function () {
            var url = File_Download_Url;
            var img = url + row.handheldPhotos;
            var img1 = url + row.fontCard;
            var img2 = url + row.reverseCard;
            $("#handheldPhotos").attr("src", img);
            $("#fontCard").attr("src", img1);
            $("#reverseCard").attr("src", img2);
            $("#cardId").val(row.cardId);
            $("#cardName").val(row.cardName);
            $("#rowId").val(row.id);

        });

    }, 'click .userDetails': function (e, value, row) {
        showModel("queryUser-div", function () {
            $("#queryCardId").val(row.cardId);
            $("#queryName").val(row.cardName);
            var url = File_Download_Url;
            var img = url + row.handheldPhotos;
            var img1 = url + row.fontCard;
            var img2 = url + row.reverseCard;
            $("#photo").attr("src", img);
            $("#card").attr("src", img1);
            $("#photoCon").attr("src", img2);
        });

    }, 'click .company': function (e, value, row) {
        showModel("queryCompany-div", function () {
            var table = $("#company");
            var url = $().AbsUrlParse("/web/user/queryCompanyList?id=" + row.id);
            table.bootstrapTable("refresh", {url: url});
        });

    },
    'click .order': function (e, value, row) {
        showModel("queryOrder-div", function () {
            var table = $("#order");
            var url = $().AbsUrlParse("/web/user/queryOrderList?id=" + row.id);
            table.bootstrapTable("refresh", {url: url});
            payType = function (value, row, index) {
                if (row.payType == 1) {
                    return [
                        '<div><span >' + '支付宝' + '</span></div> '
                    ].join('');
                } else if (row.payType == 2) {
                    return [
                        '<div ><span >' + '微信' + '</span></div> '
                    ].join('');
                } else if (row.payType == 3) {
                    return [
                        '<div ><span >' + '支付宝 + 红包' + '</span></div> '
                    ].join('');
                } else if (row.payType == 4) {
                    return [
                        '<div ><span >' + '微信 + 红包' + '</span></div> '
                    ].join('');
                } else {
                    return [
                        '<div ><span >' + '红包抵扣' + '</span></div> '
                    ].join('');
                }
            };
            orderState = function (value, row, index) {
                if (row.orderState == 1) {
                    return [
                        '<div><span>待付款</span></div>'
                    ].join('');
                } else if (row.orderState == 2) {
                    return [
                        '<div><span>待接单</span></div>'
                    ].join('');
                } else if (row.orderState == 3) {
                    return [
                        '<div><span>待配送</span></div>'
                    ].join('');
                } else if (row.orderState == 4) {
                    return [
                        '<div><span>配送中</span></div>'
                    ].join('');
                } else if (row.orderState == 5) {
                    return [
                        '<div><span>待评价</span></div>'
                    ].join('');
                } else if (row.orderState == 6) {
                    return [
                        '<div><span>已完结</span></div>'
                    ].join('');
                } else if (row.orderState == 7) {
                    return [
                        '<div><span>申请退款</span></div>'
                    ].join('');
                } else if (row.orderState == 8) {
                    return [
                        '<div><span>拒绝接单</span></div>'
                    ].join('');
                } else if (row.orderState == 9) {
                    return [
                        '<div><span>同意退款</span></div>'
                    ].join('');
                } else if (row.orderState == 10) {
                    return [
                        '<div><span>拒绝退款</span></div>'
                    ].join('');
                } else if (row.orderState == 11) {
                    return [
                        '<div><span>用户取消</span></div>'
                    ].join('');
                } else if (row.orderState == 12) {
                    return [
                        '<div><span>待取货</span></div>'
                    ].join('');
                }
            }
        });
    },
    'click .details': function (e, value, row) {
        showModel("queryDetails-div", function () {
            var table = $("#details");
            var url = $().AbsUrlParse("/web/user/queryMoneyDetails?id=" + row.id);
            table.bootstrapTable("refresh", {url: url});
            sourceType = function (value, row, index) {
                if (row.sourceType == 1) {
                    return [
                        '<div><span>下单</span></div>'
                    ].join('');
                } else if (row.sourceType == 2) {
                    return [
                        '<div><span>退款</span></div>'
                    ].join('');
                } else if (row.sourceType == 3) {
                    return [
                        '<div><span>退货</span></div>'
                    ].join('');
                } else if (row.sourceType == 4) {
                    return [
                        '<div><span>拒绝接单</span></div>'
                    ].join('');
                }
            }
        });
    },
    'click .update': function (e, value, row) {
        showModel("updateUser-div", function () {
            $("input").attr("value", "");
            $("#id").val(row.id);
            $("#account").val(row.account);
            $("#name").val(row.nickName);
            var img = File_Download_Url + row.headPic;
            $("#Pic").attr("src", "" + img);
            $("#Pic").attr("img-path", row.headPic);
        });
    },
    'click .reset': function (e, value, row) {
        showModel("resetPassword-div", function () {
            $("#ids").val(row.id);
            $("#pwd").val(123456);
        });
    },
    'click .remove': function (e, value, row) {
        comment.showOperationTips("确定删除吗？", function () {
            $.post($().AbsUrlParse('/web/user/updateStatus'), {
                id: row.id, state: 0
            }, function (data) {
                if (data.state == true) {
                    if (data.res.code == 40000) {
                        hint("删除成功");
                        var table = $("#table");
                        table.bootstrapTable("refresh");
                    } else {
                        hint(data.res.msg);
                    }
                } else {
                    hint(data.msg);
                }
            });
        });
    }
};

User.accountOrName = function (value, row, index) {
    var account = row.account;
    var nickName = row.nickName;
    if (account != null && account != '') {
        return [
            '<div><span>' + nickName + '</span></div>'
        ].join('');
    } else if (nickName != null && nickName != '') {
        return [
            '<div><span>' + account + '</span></div>'
        ].join('');
    }
};
User.isCertificate = function (value, row, index) {
    if (row.isCertification == 0) {
        return [
            '<div><span> 未认证 </span></div>'
        ].join('');
    } else if (row.isCertification == 1 && row.isCertificate == 0) {
        return [
            '<div><span>待审核</span></div>'
        ].join('');
    } else {
        //noinspection JSAnnotator
        return [
            '<div><span>认证通过</span></div>'
        ].join('');
    }
};
User.year = function (value, row, index) {

    return [
        '<div><span> ' + row.yearNumber + '年' + row.monthNumber + '月 </span></div>'
    ].join('');

};
User.OrderStatus = function (value, row, index) {
    if (row.status == 1) {
        return [
            '<div><span>未获取</span></div>'
        ].join('');
    } else {
        return [
            '<div><span>已获取</span></div>'
        ].join('');
    }
};

window.operateEventes = {
    'click .states': function (e, value, row) {
        comment.showOperationTips("确定要禁用用户吗？", function () {
            $.post($().AbsUrlParse('/web/user/updateStatus'), {id: row.id, state: 2}, function (data) {
                if (data.state == true) {
                    if (data.res.code == 40000) {
                        hint("设置成功");
                        var table = $("#table");
                        table.bootstrapTable("refresh");
                    } else {
                        hint(data.res.msg);
                    }
                } else {
                    hint(data.msg);
                }
            });
        });
    },
    'click .state': function (e, value, row) {
        comment.showOperationTips("确定要启用用户吗？", function () {
            $.post($().AbsUrlParse('/web/user/updateStatus'), {id: row.id, state: 1}, function (data) {
                if (data.state == true) {
                    if (data.res.code == 40000) {
                        hint("设置成功");
                        var table = $("#table");
                        table.bootstrapTable("refresh");
                    } else {
                        hint(data.res.msg);
                    }
                } else {
                    hint(data.msg);
                }
            });
        });
    }
};
window.companyOperateEvents = {
    'click .companyOrder': function (e, value, row) {
        showModel("queryCompanyOrder-div", function () {
            var table = $("#companyOrder");
            var url = $().AbsUrlParse("/web/user/queryCompanyOrderList?id=" + row.id);
            table.bootstrapTable("refresh", {url: url});
        });

    }
};


/**
 *执行修改保存操作
 * */
bindClick("button[data-event=submit-updateUser]", function () {
    var imagePath = $("#Pic").attr("img-path");
    $("#picture").val(imagePath);

    var form = $("#updateUseFrom");
    var v = form.valid();
    if (v == false) {
        return false;
    }
    var params = form.formToArray();
    $.post($().AbsUrlParse("/web/user/updateUserInfo"), params, function (res) {
        if (res.state && res.res.code == 40000) {
            hint('修改成功！');
            //关闭model方法
            closeModel('updateUser-div');
            var table = $("#table");
            table.bootstrapTable("refresh");
        } else {
            if (res.state == false) {
                hint(res.msg);
            } else {
                hint(res.res.msg);
            }
        }
    });
});


/**
 *重置密码
 *
 * */
bindClick("button[data-event=submit-resetPassword]", function () {

    var form = $("#resetFrom");
    var v = form.valid();
    if (v == false) {
        return false;
    }
    var params = form.formToArray();
    $.post($().AbsUrlParse("/web/user/resetPassword"), params, function (res) {
        if (res.state && res.res.code == 40000) {
            hint('重置成功');
            //关闭model方法
            closeModel('resetPassword-div');
            var table = $("#table");
            table.bootstrapTable("refresh");
        } else {
            if (res.state == false) {
                hint(res.msg);
            } else {
                hint(res.res.msg);
            }
        }
    });
});
/**
 *审核驳回
 *
 * */
bindClick("button[data-event=submit-pass]", function () {
    var rowId = $("#rowId").val();
    $.post($().AbsUrlParse("/web/user/pass"), {id: rowId, type: 1}, function (res) {
        if (res.state && res.res.code == 40000) {
            //关闭model方法
            // closeModel('userDetail-div');
            var table = $("#table");
            table.bootstrapTable("refresh");
            return;
        } else {
            if (res.state == false) {
                hint(res.msg);
            } else {
                hint(res.res.msg);
            }
        }
    });
});
/**
 *实名认证驳回
 *
 * */
bindClick("button[data-event=submit-reject]", function () {
    var rowId = $("#rowId").val();
    $.post($().AbsUrlParse("/web/user/pass"), {id: rowId, type: 2}, function (res) {
        if (res.state && res.res.code == 40000) {
            //关闭model方法
            // closeModel('userDetail-div');
            var table = $("#table");
            table.bootstrapTable("refresh");
            return;
        } else {
            if (res.state == false) {
                hint(res.msg);
            } else {
                hint(res.res.msg);
            }
        }
    });
});

var queryUserInfo = function () {
    var name = $("#nick").val();
    //注册时间
    var created = $("#start").val();
    var obj = created.split("-");
    var startCreate = obj[0] + "-" + obj[1] + "-" + obj[2];
    if (obj[1] == undefined) {
        startCreate = "";
    }
    var endCreate = obj[3] + "-" + obj[4] + "-" + obj[5];
    if (obj[4] == undefined) {
        endCreate = "";
    }
    //最后登录时间
    var lastTime = $("#end").val();
    var obj1 = lastTime.split("-");
    var startTime = obj1[0] + "-" + obj1[1] + "-" + obj1[2];
    if (obj1[1] == undefined) {
        startTime = "";
    }
    var endTime = obj1[3] + "-" + obj1[4] + "-" + obj1[5];
    if (obj1[4] == undefined) {
        endTime = "";
    }
    var table = $("#table");
    var url = $().AbsUrlParse("/web/user/queryUserList?name=" + name + "&startCreate=" + startCreate +
        "&endCreate=" + endCreate + "&startTime=" + startTime + "&endTime=" + endTime);
    table.bootstrapTable("refresh", {url: url});
};

User.status = function (value, row, index) {
    if (row.userState == 1) {
        return [

            '<div><span><a class="states" href="javascript:void(0)" >启用</a></span></div>'
        ].join('');
    } else if (row.userState == 2) {
        return [

            '<div><span><a class="state" href="javascript:void(0)" style="color: red" >禁用</a></span></div>'
        ].join('');
    }
};

function pass(e, value, row) {
    var rowId = $("#rowId").val();
    $.post($().AbsUrlParse("/web/user/pass"), {id: rowId, type: 1}, function (res) {
        if (res.state && res.res.code == 40000) {
            //关闭model方法
            // closeModel('userDetail-div');
            var table = $("#table");
            table.bootstrapTable("refresh");
            return;
        } else {
            if (res.state == false) {
                hint(res.msg);
            } else {
                hint(res.res.msg);
            }
        }
    });
}
function initDateRange() {
    $(".rangedate").daterangepicker({
        "autoApply": true,
        "locale": {
            "applyLabel": "确定",
            "cancelLabel": "取消",
            "fromLabel": "开始",
            "toLabel": "结束",
            "customRangeLabel": "Custom",
            "weekLabel": "周",
            "daysOfWeek": [
                "日",
                "一",
                "二",
                "三",
                "四",
                "五",
                "六"
            ],
            "monthNames": [
                "1月",
                "2月",
                "3月",
                "4月",
                "5月",
                "6月",
                "7月",
                "8月",
                "9月",
                "10月",
                "11月",
                "12月"
            ],
            "cancelLabel": '清空',
            "opens": "right"
        },
        "format": "YYYY-MM-DD"
    }).on('cancel.daterangepicker', function (ev, picker) {
        $(this).val('');
    });
}

//初始化页面的时候运行该函数
$(function () {
    initDateRange();
});